/**
* 头部菜单
*/ 
<template>
  <div>
    <el-menu
      class="el-menu-demo"
      mode="horizontal"
      background-color="#334157"
      text-color="#fff"
      active-text-color="#fff"
    >
      <el-button class="buttonimg">
        <img
          class="showimg"
          :src="collapsed ? imgsq : imgshow"
          @click="toggle(collapsed)"
        />
      </el-button>
      <el-submenu index="2" class="submenu">
        <!-- <template slot="title">{{user.userRealName}}</template> -->
        <template slot="title">{{ user.name }}</template>
        <el-menu-item index="2-1" @click="showDialog()">修改密码</el-menu-item>

        <!-- <el-menu-item @click="content()" index="2-2">个人中心</el-menu-item> -->
        <el-menu-item @click="exit()" index="2-2">退出</el-menu-item>
      </el-submenu>
    </el-menu>

    <el-dialog title="修改密码" :visible.sync="dialogVisible" width="500px">
      <el-form :rules="rules" ref="passwordForm">
        <el-form-item label="新密码:">
          <el-input
            size="small"
            type="password"
            v-model="passwordForm.newVal"
            placeholder="输入新密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码:">
          <el-input
            size="small"
            type="password"
            v-model="passwordForm.checkVal"
            placeholder="输入确认密码"
          ></el-input>
        </el-form-item>
        <el-form-item style="width: 100%">
          <el-button
            type="primary"
            style="width: 100%"
            @click="submitForm('passwordForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import axios from "axios";
export default {
  name: "navcon",
  data() {
    return {
      dialogVisible: false,
      passwordForm: {
        oldVal: "",
        newVal: "",
      },
      rules: {
        newVal: [
          { required: true, message: "请输入确认密码", trigger: "blur" },
        ],
        checkVal: [
          { required: true, message: "请输入新密码", trigger: "blur" },
        ],
        // code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      collapsed: true,
      imgshow: require("../assets/img/show.png"),
      imgsq: require("../assets/img/sq.png"),
      user: JSON.parse(localStorage.getItem("userdata")),
    };
  },
  // 创建完毕状态(里面是操作)
  created() {
    this.user = JSON.parse(localStorage.getItem("userdata"));
  },
  methods: {
    showDialog() {
      this.dialogVisible = true;
    },
    submitForm(formName) {
      if (this.passwordForm.checkVal !== this.passwordForm.newVal) {
        this.$message.error("两次输入的密码不一致！");
        return;
      }
      this.user.password = this.passwordForm.newVal;
      this.$confirm("确定修改密码?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 发送请求，修改密码
          axios
            .post("/api/employee/updateEmployeeById", this.user)
            .then((res) => {
              if (res.data.code === 200) {
                this.$message.success("密码修改成功！");
                this.logout();
              } else {
                this.$message.info(res.message);
              }
            })
            .catch((err) => {
              this.loading = false;
              this.$message.error("修改密码失败，请稍后再试！");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
      console.log(this.passwordForm.newVal);
    },
    logout() {
      axios
        .get("/api/logout")
        .then((res) => {
          return res.data;
        })
        .then((res) => {
          if (res.code === 200) {
            //如果请求成功就让他2秒跳转路由
            setTimeout(() => {
              this.$store.commit("logout", "false");
              this.$router.push({ path: "/login" });
              this.$message({
                type: "success",
                message: "已退出登录!",
              });
            }, 1000);
          } else {
            this.$message.error(res.msg);
            return false;
          }
        });
    },
    // 退出登录
    exit() {
      this.$confirm("退出登录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.logout();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    // 切换显示
    toggle(showtype) {
      this.collapsed = !showtype;
      this.$root.Bus.$emit("toggle", this.collapsed);
    },
  },
};
</script>
<style scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  border: none;
}
.submenu {
  float: right;
}
.buttonimg {
  height: 60px;
  background-color: transparent;
  border: none;
}
.showimg {
  width: 26px;
  height: 26px;
  position: absolute;
  top: 17px;
  left: 17px;
}
.showimg:active {
  border: none;
}
</style>